<?php

namespace frontend\modules\articlesite\controllers;

use common\components\Upload;
use common\models\articlesite\ArticlesiteArticle;
use common\models\articlesite\ArticlesiteCategory;
use common\models\shop\ShopSlide;
use frontend\controllers\BaseMobileController;
use yii\data\Pagination;
use yii\helpers\Url;
use yii\web\Response;


/**
 * Class FarmerHoseController
 * @package frontend\modules\business\controllers
 * 文章关控制器
 */
class SiteController extends BaseMobileController
{


    /**
     * @return string
     * 微站首页
     */
    public function actionIndex()
    {
        $slids = ShopSlide::find()->select('picture')->where('status=:status and type=:type',[':status'=>ShopSlide::ACTIVE_STATUS,':type'=>ShopSlide::TYPE_ARTICLE])->asArray()->all();
        $banner = [];
        foreach ($slids as $slid => $row) {
            $banner[] = Upload::checkGetUrl($row['picture']);
        }


        $category = ArticlesiteCategory::find()->select('id,name')->where(['status'=>1,'usefor'=>ArticlesiteCategory::USEFOR_JIANJIE])->asArray()->all();
        foreach ($category as $key => $row)
        {
            $category[$key]['url'] = Url::to(['/articlesite/detail','cid' => $row['id']]);
        }


        $article = ArticlesiteArticle::find()->select('thumb as img,id,descripation as content')
            ->where('usefor=:usefor and status=:sta and isindex=:isindex',[':usefor'=>ArticlesiteArticle::USEFOR_JIANJIE,':isindex'=>ArticlesiteArticle::ISINDEX,':sta'=>ArticlesiteArticle::ARTICLE_SHOW])
            ->limit(10)
            ->asArray()
            ->all();
        foreach ($article as $key => $row)
        {
            $article[$key]['url'] = Url::to(['/articlesite/site/detail','id' => $row['id']]);
            $article[$key]['img'] = Upload::checkGetUrl($row['img']);
        }

        return $this->render('@template/mobile/article_index.html',[
            'banner' => \GuzzleHttp\json_encode($banner),
            'article' => \GuzzleHttp\json_encode($article),
            'category' => $category
        ]);
    }


    /**
     * @param $cateid
     * @return string
     * 文章的列表页
     */
    public function actionList($cateid)
    {
        $category = ArticlesiteCategory::find()->select('id,name')->where(['status'=>1,'usefor'=>ArticlesiteCategory::USEFOR_JIANJIE])->asArray()->all();
        foreach ($category as $key => $row)
        {
            $category[$key]['url'] = Url::to(['/articlesite/detail','cid' => $row['id']]);
        }

        $article =  ArticlesiteArticle::find()
            ->select('id,thumb as img,descripation as content')
            ->where('usefor=:usefor and status=:sta and cateid=:cid',[':usefor'=>ArticlesiteArticle::USEFOR_JIANJIE,':sta'=>ArticlesiteArticle::ARTICLE_SHOW,':cid'=>$cateid])
            ->limit(10)
            ->asArray()
            ->all();
        if($article){
            foreach ($article as $key => $row)
            {
                $article[$key]['url'] = Url::to(['/articlesite/site/detail','id' => $row['id']]);
                $article[$key]['img'] = Upload::checkGetUrl($row['img']);
            }
        }


        return $this->render('@template/mobile/article_list.html',[
            'article' => \GuzzleHttp\json_encode($article),
            'category' => $category
        ]);
    }


    /**
     * @param $id
     * @return string
     * 文章的详情页
     */
    public function actionDetail($id)
    {
        $article_detail = ArticlesiteArticle::find()->where('id=:id',['id'=>$id])->asArray()->one();
        return $this->render('@template/mobile/article_detail.html',[
            'detail' => $article_detail
        ]);
    }


    /**
     * @return array|\yii\db\ActiveRecord[]
     * 文章的加载更多
     */
    public function actionLoadMore()
    {
        $pagesize  = 10;
        $pindex = \Yii::$app->request->post('pindex');
        $pindex = $pindex?$pindex:1;
        $cateid = \Yii::$app->request->post('cateid');

        $query =  ArticlesiteArticle::find()
            ->select('id,thumb as img,descripation as content')
            ->limit($pagesize)->offset($pagesize*($pindex-1))
            ->asArray();

        if(!$cateid){
            $article = $query->where('usefor=:usefor and status=:sta and isindex=:isindex',[':usefor'=>ArticlesiteArticle::USEFOR_JIANJIE,':isindex'=>ArticlesiteArticle::ISINDEX,':sta'=>ArticlesiteArticle::ARTICLE_SHOW])->all();
        }else{
            $article = $query->where('usefor=:usefor and status=:sta and isindex=:isindex and cateid=:cid',[':usefor'=>ArticlesiteArticle::USEFOR_JIANJIE,':isindex'=>ArticlesiteArticle::ISINDEX,':sta'=>ArticlesiteArticle::ARTICLE_SHOW,':cid'=>$cateid])->all();
        }

        if($article){
            foreach ($article as $key => $row)
            {
                $article[$key]['url'] = Url::to(['/articlesite/site/detail','id' => $row['id']]);
                $article[$key]['img'] = Upload::checkGetUrl($row['img']);
            }
        }

        \Yii::$app->response->format = Response::FORMAT_JSON;

        return $article;
    }


    /**
     * @return array|\yii\db\ActiveRecord[]
     * 搜索文章
     */
    public function actionSearch()
    {
        $kwd = \Yii::$app->request->post('keywords');

        \Yii::$app->response->format = Response::FORMAT_JSON;

        if(!$kwd){
            return [];
        }

        $article = ArticlesiteArticle::find()
            ->select('thumb as img,id,descripation as content')
            ->where('usefor=:usefor and status=:sta and title like :title',[':usefor'=>ArticlesiteArticle::USEFOR_JIANJIE,':sta'=>ArticlesiteArticle::ARTICLE_SHOW,':title'=>"%$kwd%"])
            ->asArray()
            ->all();
        if($article){
            foreach ($article as $key => $row)
            {
                $article[$key]['url'] = Url::to(['/articlesite/site/detail','id' => $row['id']]);
                $article[$key]['img'] = Upload::checkGetUrl($row['img']);
            }
        }

        return $article;

    }



}